package com.example.ddcoursequery;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.text.method.DigitsKeyListener;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class QueryCourseByImg extends Activity {
    private String id;
    private String name;
    private String semester;

    private ImageView imageView;
    private EditText validateCodeValue;

    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_querycoursebyimg);
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        name = intent.getStringExtra("name");
        semester = intent.getStringExtra("semester");

        TextView textView = (TextView)findViewById(R.id.teachersInfoText);
        textView.setText("工号:"+id+"          名字:"+name);

        imageView = (ImageView)findViewById(R.id.validateImageView);
        initImgCode();

        validateCodeValue = (EditText) findViewById(R.id.validateImageCode);
        validateCodeValue.setKeyListener(new DigitsKeyListener() {
            @Override
            public int getInputType() {
                return InputType.TYPE_TEXT_VARIATION_PASSWORD;
            }

            @Override
            protected char[] getAcceptedChars() {
                char[] data = getResources().getString(R.string.login_only_can_input).toCharArray();
                return data;
            }
        });

        button = (Button)findViewById(R.id.submitButton);
    }

    public void queryByImageCode(View view){
        final String imgCodeStr = validateCodeValue.getText().toString();
        if(!TextUtils.isEmpty(imgCodeStr) && imgCodeStr.length() == 4){
            Log.i("lyj","QueryCourseByImg queryByImageCode click");
            new Thread(){
                @Override
                public void run() {
                    HttpData httpData = new HttpData();
                    int flag = httpData.getCoursesByImageCode(id, semester, imgCodeStr);
                    if(flag == 0){
                        turnToShowCourse();
                        CacheTeacher.titleList.add(0,name);
                    }else{
                        turnToShowQueryCourseByTeacher();
                    }

                }
            }.start();
        }
    }

    private void turnToShowQueryCourseByTeacher() {
        Intent intent = new Intent(QueryCourseByImg.this, QueryCourseByTeacher.class);
        intent.putExtra("error","该老师还没有排课");
        startActivity(intent);
    }

    private void turnToShowCourse() {
        Intent intent = new Intent(QueryCourseByImg.this, ShowCourseActivity.class);
        startActivity(intent);
    }

    //加载校验码
    private void initImgCode() {
        new Thread() {
            @Override
            public void run() {
                HttpData httpData = new HttpData();
                final Bitmap bitmap = BitmapFactory.decodeFile(httpData.getValidateCodeImg());
                imageView.post(new Runnable() {
                    @Override
                    public void run() {
                        imageView.setImageBitmap(bitmap);
                    }
                });

            }
        }.start();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_querycoursebyimg, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
